package com.station.search.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.station.search.common.domain.WebResponse;
import com.station.search.common.domain.WebResponseBuilder;
import com.station.search.constant.DeleteEnum;
import com.station.search.domain.StationQuestion;
import com.station.search.domain.converter.StationQuestionConverter;
import com.station.search.domain.vo.StationQuestionVO;
import com.station.search.service.StationQuestionService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: JT
 * @date: 2021/8/31
 * @Title: 台站问题 Web处理器
 */
@RestController
@RequestMapping
public class StationQuestionController {

    @Resource
    private StationQuestionService stationQuestionService;

    @GetMapping("/stations/questions")
    public WebResponse questionRecords(
            @RequestParam(value="stationCode", required = false) String stationCode,
            @RequestParam(value="searchWord", required = false) String searchWord
    ) {
        LambdaQueryChainWrapper<StationQuestion> stationQuestionRecordsQuery = stationQuestionService.lambdaQuery();
        stationQuestionRecordsQuery.eq(StationQuestion::getIsDeleted, DeleteEnum.NOT_DELETED.getValue());
        if (StringUtils.hasText(stationCode)) {
            stationQuestionRecordsQuery.eq(StationQuestion::getStationCode, stationCode);
        }
        if (StringUtils.hasText(searchWord)) {
            stationQuestionRecordsQuery.and(
                    query -> query.like(StationQuestion::getStationCode, searchWord)
                            .or()
                            .like(StationQuestion::getStationName, searchWord)
                            .or()
                            .like(StationQuestion::getQuestion, searchWord)
            );
        }
        stationQuestionRecordsQuery.orderByDesc(StationQuestion::getQuestionDate);

        List<StationQuestion> stationQuestions = stationQuestionRecordsQuery.list();
        return WebResponseBuilder.ok("获取台站问题列表")
                .data(
                        stationQuestions.stream()
                                .map((question) -> StationQuestionConverter.INSTANCE.doToDto(question))
                                .map(record -> {
                                    if(Objects.isNull(record.getQuestion())) {
                                        return record;
                                    }
                                    record.setQuestion(record.getQuestion().replace("\n", ""));
                                    if (record.getQuestion().length() <= 10) {
                                        return record;
                                    }
                                    record.setQuestion(record.getQuestion().substring(0, 10) + "...");
                                    return record;
                                })
                                .collect(Collectors.toList())
                );
    }

    @GetMapping("/stations/questions/{questionId}")
    public WebResponse questionRecordById(@PathVariable("questionId") String questionId) {
        StationQuestion stationQuestionQuery = new StationQuestion();
        stationQuestionQuery.setId(questionId);
        StationQuestion stationQuestion = stationQuestionService.getOne(new QueryWrapper<>(stationQuestionQuery));
        return WebResponseBuilder.ok(
                "获取台站[{}/{}]问题详情[{}]成功！",stationQuestion.getStationCode(), stationQuestion.getStationName(), stationQuestion.getId()
        ).data(StationQuestionConverter.INSTANCE.doToDto(stationQuestion));
    }

    @PostMapping("/stations/questions")
    public WebResponse addStationQuestion(@RequestBody StationQuestionVO stationQuestionVO) {
        StationQuestion stationQuestion = StationQuestionConverter.INSTANCE.voToDo(stationQuestionVO);
        stationQuestionService.saveOrUpdate(stationQuestion);
        if (StringUtils.isEmpty(stationQuestion.getId())) {
            return WebResponseBuilder.ok("添加台站[{}]抢修记录[{}]成功！", stationQuestion.getStationCode(), stationQuestion.getId());
        }
        return WebResponseBuilder.ok("修正台站[{}]巡检记录[{}]成功！", stationQuestion.getStationCode(), stationQuestion.getId());
    }

    @PutMapping("/stations/questions/{questionId}")
    public WebResponse modifyStationQuestion(@PathVariable("questionId") String questionId, @RequestBody StationQuestionVO stationQuestionVO) {
        StationQuestion stationQuestion = StationQuestionConverter.INSTANCE.voToDo(stationQuestionVO);
        stationQuestion.setId(questionId);
        stationQuestionService.updateById(stationQuestion);
        return WebResponseBuilder.ok("修改台站[{}]已提交问题成功！", stationQuestion.getStationCode());
    }

    @GetMapping("/stations/{stationId}/questions")
    public WebResponse queryStationQuestions(@PathVariable("stationId") String stationId) {
        List<StationQuestion> stationQuestions = stationQuestionService.queryStationQuestions(stationId);
        return WebResponseBuilder.ok(
                "获取台站[{}]问题列表成功", stationId
        ).data(stationQuestions);
    }

    @DeleteMapping("/stations/questions/{questionId}")
    public WebResponse deleteStationQuestion(@PathVariable("questionId")String questionId) {
        stationQuestionService.deleteQuestionById(questionId);
        return WebResponseBuilder.ok("删除台题成功!问题ID：[{}]", questionId);
    }
}
